<template>
	<div class="app-container">
		<div ref="box1" class="flex-center margin-bottom-10">
			<el-radio-group v-model="searchParams.dutyState" @change="onStateChange">
				<el-radio-button v-for="(item, index) in upkeepStateList" :key="index" :label="item.id">
					<el-badge v-if="item.count" :value="item.count" :max="99">
						<span class="margin-right-20">{{ item.name }}</span>
					</el-badge>
					<span v-else class="margin-right-10">{{ item.name }}</span>
				</el-radio-button>
			</el-radio-group>
		</div>
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<jk-add-button :permission-prefix="prefix" class="margin-5-0 item-gap" @click="onAddBtn"></jk-add-button>
				</div>
				<div class="flex-end-center-wrap">
					<jk-select v-model="searchParams.auditState" :option-list="auditStateList" placeholder="状态" clearable size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-select v-model="searchParams.upkeepTypeId" :clearable="true" :option-list="upkeepTypeList" placeholder="保养类别" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-search-input v-model="searchParams.empName" placeholder="人员名称" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table ref="jkTable" :loading="tableLoading" :data="tableData" :height="tableHeight - 30" :cell-style="cellStyle">
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<jk-common-status v-if="item.keyName === 'auditState'" :status="scope.row.auditState"></jk-common-status>
						<div v-else-if="item.keyName === 'machineList'" class="flex-start-center-wrap">
							{{ scope.row.machineList ? scope.row.machineList.map(x => x.machineName).join('、') : '' }}
						</div>
						<div v-else-if="item.keyName === 'upkeepTypeList'" class="flex-start-center-wrap">
							{{ scope.row.upkeepTypeList ? scope.row.upkeepTypeList.map(x => x.name).join('、') : '' }}
						</div>
						<div v-else-if="item.keyName === 'processList'" class="flex-start-center-wrap">
							{{ scope.row.userInfo && scope.row.userInfo.processList ? scope.row.userInfo.processList.map(x => x.processName).join('、') : '' }}
						</div>
						<div v-else-if="item.keyName === 'userName'" class="flex-start-center-wrap">
							{{ scope.row.userInfo ? scope.row.userInfo.username : '无' }}
						</div>
						<div v-else-if="item.keyName === 'dutyState'" class="flex-start-center-wrap">
							{{ scope.row.dutyState === 0 ? '保养' : '巡检' }}
						</div>
						<span v-else>{{ scope.row[item.keyName] }}</span>
					</template>
				</vxe-column>
				<vxe-column title="操作" align="center" fixed="right" :width="250">
					<template slot-scope="scope">
						<jk-operation-bar
							:scope="scope"
							api-prefix="machinemanager/upkeep-personnel"
							:permission-prefix="prefix"
							@on-detail="onDetailBtn"
							@on-delete="onDeleteBtn"
							@on-edit="onEditBtn"
							@on-refresh="getListRequest"
						></jk-operation-bar>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<save-dialog
			:page-type="pageType"
			:data-id="dataId"
			:dialog-state="saveDialogState"
			@on-visible-change="onVisibleChange"
			@on-finish="onFinish"
		></save-dialog>
	</div>
</template>

<script>
    import { auditStateList } from '@/utils/jk/status-option-list';
    import saveDialog from './save-dialog';
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '../../../../mixins/getMenuColumn';
    import handlerCache from '../../../../mixins/handler-cache';
    import upkeepMixin from '../upkeepItemPlan/upkeep';
    import { machineManagerUpkeepTypePage } from '../../../../api/machine/upkeepCategory';
    import { machineManagerUpkeepPersonnelPage } from '../../../../api/machine/upkeepEmp';
    export default {
        name: 'UpkeepEmpAndMachine',
        components: {
            saveDialog
        },
        mixins: [calHeight, getMenuColumn, handlerCache, upkeepMixin],
        data() {
            return {
                prefix: 'machinemanager:upkeepPersonnel',
                apiItem: {
                    'page': machineManagerUpkeepPersonnelPage
                },
                pageType: '',
                upkeepStateList: [
                    { id: 0, name: '保养', color: '#2b85e4' },
                    { id: 1, name: '巡检', color: '#ff9900' },
                    { id: null, name: '全部', color: '#19be6b' }
                ],
                auditStateList: auditStateList,
                dataId: null,
                saveDialogState: false,
                tableLoading: true,
                tableData: [],
                upkeepTypeList: [],
                totalCount: 0,
                searchParams: {
                    pageNo: 1,
                    pageSize: pageConfig.pageSize,
                    dutyState: 0,
                    empName: null,
                    upkeepTypeId: null,
                    auditState: null,
                    processId: null
                }
            };
        },
        methods: {
            cellStyle({ row, column }) {
                if (column.field === 'empName') {
                    if (row.isAlternate === true) {
                        return {
                            backgroundColor: '#ed4014',
                            color: '#ffffff'
                        };
                    } else {
                        return {
                            backgroundColor: ''
                        };
                    }
                }
            },
            onStateChange() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(pageIndex, pageSize) {
                this.searchParams.pageNo = pageIndex;
                this.searchParams.pageSize = pageSize;
                this.getListRequest();
            },
            onDetailBtn(e) {
                this.pageType = 'detail';
                this.dataId = e.id;
                this.saveDialogState = true;
            },
            onFinish() {
                this.saveDialogState = false;
                this.getListRequest();
            },
            onVisibleChange(e) {
                if (!e) {
                    this.dataId = null;
                }
                this.saveDialogState = e;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                this.apiItem.page(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.totalCount = 1;
                this.getListRequest();
            },
            /** 新增按钮操作 */
            onAddBtn() {
                this.pageType = 'add';
                this.saveDialogState = true;
            },
            /** 修改按钮操作 */
            onEditBtn(row) {
                this.pageType = 'edit';
                this.dataId = row.id;
                this.saveDialogState = true;
            },

            /** 删除按钮操作 */
            onDeleteBtn(row) {
                this.apiItem.delete({ id: row.id }).then((res) => {
                    if (res.data.status === 200) {
                        this.getListRequest();
                        this.$tipsBar(this, 'delete');
                    }
                }).catch(() => {});
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            getUpkeepTypeListRequest() {
                return machineManagerUpkeepTypePage({ auditState: 1 }).then(res => {
                    if (res.data.status === 200) {
                        this.upkeepTypeList = res.data.res || [];
                    }
                });
            },
            async getDependentData() {
                await this.getMenuColumnListRequest();
                await this.getUpkeepTypeListRequest();
                await this.getListRequest();
            }
        },
        mounted() {
            this.getDependentData();
        }
    };
</script>
